import 'dart:math';

import 'package:bot_toast/src/basis.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/util/api_router.dart';
import 'package:url_launcher/url_launcher.dart';

class UpdateDialog extends StatefulWidget {
  final CancelFunc cancelFunc;
  final String? updateMessage;
  const UpdateDialog(this.cancelFunc, this.updateMessage, {super.key});

  @override
  State<UpdateDialog> createState() => _UpdateDialogState();
}

const shakeCount = 4;
const shakeDuration = Duration(milliseconds: 500);

class _UpdateDialogState extends State<UpdateDialog>
    with TickerProviderStateMixin {
  late final AnimationController _shakeController =
      AnimationController(vsync: this, duration: shakeDuration);
  @override
  void initState() {
    _shakeController.addListener(() {
      if (_shakeController.status == AnimationStatus.completed) {
        _shakeController.reset();
      }
    });
    super.initState();
    shake();
  }

  shake() {
    _shakeController.forward();
  }

  _launchURL() async {
    String url = "${ApiRouter.getHttpServer()}open/api/app/dowinfo";
    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url));
    } else {
      throw '无法打开 $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        color: const Color.fromARGB(153, 0, 0, 0),
        child: Center(
          child: AnimatedBuilder(
              animation: _shakeController,
              builder: (context, child) {
                final sineValue =
                    sin(shakeCount * 2 * pi * _shakeController.value);
                return Transform.translate(
                  offset: Offset(sineValue * 10, 0),
                  child: child,
                );
              },
              child: Container(
                width: 300.w,
                height: 300.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  /*  boxShadow: [
                  BoxShadow(
                    color:
                        const Color.fromARGB(146, 19, 18, 18).withOpacity(0.5),
                    spreadRadius: 5,
                    blurRadius: 7,
                    offset: Offset(0, 10.w), // changes position of shadow
                  ),
                ], */
                ),
                child: Column(
                  children: [
                    Image.asset(
                      'assets/images/system/update_bg_app_top.png',
                      fit: BoxFit.fill,
                    ),
                    Expanded(
                      child: Container(
                        width: double.infinity,
                        color: Colors.white,
                        padding: EdgeInsets.all(10.w),
                        child: SingleChildScrollView(
                          child: Text(
                            widget.updateMessage ?? "发现新的版本",
                            style: TextStyle(
                                color: Colors.black,
                                fontSize: 12.w,
                                fontWeight: FontWeight.w600),
                          ),
                        ),
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      padding: EdgeInsets.only(bottom: 5.w),
                      child: Center(
                          child: TextButton(
                        style: ButtonStyle(
                          backgroundColor:
                              MaterialStateProperty.resolveWith((states) {
                            //设置按下时的背景颜色
                            if (states.contains(MaterialState.pressed)) {
                              return Colors.blue[200];
                            }
                            //默认不使用背景颜色
                            return const Color.fromARGB(255, 32, 150, 247);
                          }),
                          shape:
                              MaterialStateProperty.all<RoundedRectangleBorder>(
                            RoundedRectangleBorder(
                              borderRadius:
                                  BorderRadius.circular(10.r), // 这里设置为 0 就会是正方形
                            ),
                          ),
                          elevation: MaterialStateProperty.all(0),
                        ),
                        onPressed: () {
                          _launchURL();
                        },
                        child: Text(
                          "立即更新",
                          style: TextStyle(
                              fontSize: 12.w,
                              fontWeight: FontWeight.w800,
                              color: Colors.white),
                        ),
                      )),
                    ),
                    Center(
                      child: Container(
                        margin: EdgeInsets.only(top: 5.w),
                        width: 2.w,
                        height: 10.w,
                        color: Colors.white,
                      ),
                    ),
                    /*  VerticalDivider(
                      width: 20.w, color: Colors.white, thickness: 100.w), */
                    Container(
                      margin: EdgeInsets.only(top: 5.w),
                      child: InkWell(
                          onTap: () {
                            widget.cancelFunc();
                          },
                          child: Icon(
                            Icons.highlight_off,
                            size: 30.w,
                            color: Colors.white,
                          )),
                    )
                    /*   Container(
              color: Colors.white,
              child: Center(
                child: OutlinedButton(
                  style: ButtonStyle(
                    /* minimumSize: MaterialStateProperty.all(
                      Size(0, ScreenUtil().screenHeight / 8)), */
                    backgroundColor:
                        MaterialStateProperty.resolveWith((states) {
                      //设置按下时的背景颜色
                      if (states.contains(MaterialState.pressed)) {
                        return Colors.blue[200];
                      }
                      //默认不使用背景颜色
                      return Colors.blue[200];
                    }),
                    shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                      RoundedRectangleBorder(
                        borderRadius:
                            BorderRadius.circular(1.0), // 这里设置为 0 就会是正方形
                      ),
                    ),
                    elevation: MaterialStateProperty.all(0),
                  ),
                  onPressed: () {
                    cancelFunc();
                  },
                  child: Text(
                    "立即更新",
                    maxLines: 1,
                    style: TextStyle(
                        fontSize: 15.w,
                        fontWeight: FontWeight.w800,
                        color: Colors.white),
                  ),
                ),
              ),
            ) */
                  ],
                ),
              )),
        ));
  }
}
